package com.docios.cookbook2.ui.homePager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.docios.cookbook2.ui.adapter.ViewPagerAdapter;
import com.docios.cookbook2.ui.otherPager.CommonChildActivity;
import com.docios.cookbook2.R;
import com.docios.cookbook2.databinding.ActivityHomeBinding;
import com.docios.cookbook2.ui.otherPager.FavoriteFragment;

public class HomeActivity extends AppCompatActivity {

    ActivityHomeBinding binding;
    HomeFragment homeFragment;
    FavoriteFragment favoriteFragment;
    UserFragment userFragment;
    Fragment current;
    MenuItem menuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initData();
        bottomNavOnClick();
        drawerMenuOnClick();
    }


    private void initData() {
        homeFragment = new HomeFragment();
        favoriteFragment = new FavoriteFragment();
        userFragment = new UserFragment();
//        changeFragment(null, homeFragment);
//        current = homeFragment;

        //用户信息，向fragment传值
        Bundle bundle = new Bundle();
        String phone = getIntent().getStringExtra("phone");
        bundle.putString("phone", phone);
        //viewpager2
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this, bundle);
        binding.homeVp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.bottomNavigation.getMenu().getItem(position).setChecked(true);
            }
        });
        binding.homeVp.setAdapter(viewPagerAdapter);

    }

    /*
     * 底部导航栏
     * */
    @SuppressLint("NonConstantResourceId")
    private void bottomNavOnClick() {
        binding.bottomNavigation.setOnItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.menu_home:
//                    changeFragment(current, homeFragment);
                    binding.homeVp.setCurrentItem(0);
                    break;
                case R.id.menu_video:
//                    changeFragment(current, favoriteFragment);
                    binding.homeVp.setCurrentItem(1);
                    break;
                case R.id.menu_user:
//                    changeFragment(current, userFragment);
                    binding.homeVp.setCurrentItem(2);
                    break;
            }
            item.setChecked(true);
            return false;
        });
    }

    /*
     * 抽屉菜单
     * */
    private void drawerMenuOnClick() {
        Intent intent = new Intent(this, CommonChildActivity.class);

        binding.drawerNavView.setNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.menu_drawer_history:
                    Toast.makeText(this, "功能在做啦 (T~T)", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.menu_drawer_favorite:
                    intent.putExtra("flag", "favorite");
                    startActivity(intent);
                    break;
                case R.id.menu_drawer_setting:
                    Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.menu_drawer_light_mode:
                    Toast.makeText(this, "夜间模式", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.menu_drawer_info:
                    Toast.makeText(this, "关于作者", Toast.LENGTH_SHORT).show();
                    break;

            }

            return false;
        });
    }


    /*
     * 切换页块
     * replace() 清空老碎片，显示新碎片
     * add() 不清空老碎片，显示在新碎片上层。需要调用hide和show进行隐藏和显示
     * 不能多加，需要判空
     * */
    public void changeFragment(Fragment from, Fragment to) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (!to.isAdded()) {
            if (from != null) {
                fragmentTransaction.hide(from);
            }
            fragmentTransaction.add(binding.navFragment.getId(), to).commit();
        } else {
            if (from != null) {
                fragmentTransaction.hide(from);
            }
            fragmentTransaction.show(to).commit();
        }
        current = to;
    }

    /**
     * 重写返回键方法
     * 若抽屉在打开状态，点返回键，只关抽屉，不退出程序。
     */
    @Override
    public void onBackPressed() {
        if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
            binding.drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    /*
     * 自定义抽屉开方法
     * */
    public void openDrawer() {
        binding.drawerLayout.openDrawer(GravityCompat.START);
    }

    public void finishMain() {
        finish();
    }
}